<?php
namespace backend\modules\spread\controllers;

use Yii;
use yii\filters\VerbFilter;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use backend\models\activeRecords\SpreadUrl;
use backend\modules\spread\models\SpreadUrlSearch;
use yii\web\Response;
use yii\widgets\ActiveForm;

/**
 * SpreadUrlController implements the CRUD actions for SpreadUrl model.
 */
class SpreadUrlController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all SpreadUrl models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new SpreadUrlSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider
        ]);
    }

    /**
     * Displays a single SpreadUrl model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        $model = $this->findModel($id);
        $params = Yii::$app->request->bodyParams;
        if (empty($params)) {
            return $this->renderAjax('view', [
                'model' => $model,
            ]);
        } else {
            if ($model->load($params) && $model->save()) {
                return $this->redirect('index');
            } else {
                return $this->renderAjax('view', [
                    'model' => $model,
                ]);
            }
        }
    }

    /**
     * Creates a new SpreadUrl model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new SpreadUrl();
        $model->scenario = 'create';
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect('index');
        } else {
            return $this->renderAjax('create', ['model' => $model]);
        }
    }

    /**
     * Updates an existing SpreadUrl model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $model->scenario = 'update';
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect('index');
        } else {
            return $this->renderAjax('update', ['model' => $model]);
        }
    }

    /**
     * Deletes an existing SpreadUrl model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
        return $this->redirect(['index']);
    }

    /**
     * Finds the SpreadUrl model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return SpreadUrl the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = SpreadUrl::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }


    /**
     * Asynchronous Validate Form Model
     */
    public function actionValidateForm()
    {
        $model = new SpreadUrl();
        $model->load(Yii::$app->request->post());
        Yii::$app->response->format = Response::FORMAT_JSON;
        return ActiveForm::validate($model);
    }
}
